﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VK.Casemeet.Areas
{
    public static class AreaBLL
    {
        public static AreaEntity[] GetAreaArray()
        {
            return AreaDAL.GetAreaList().ToArray();
        }

        public static AreaEntity[] GetAreaArray(int parentID)
        {
            return AreaDAL.GetAreaList(parentID).ToArray();
        }

        public static AreaEntity[] GetAreaStyleArray(int rootID, bool isSpace, int? delAreaID)
        {
            return GetAreaStyleArray(rootID, 1, isSpace, delAreaID);
        }

        public static AreaEntity[] GetAreaStyleArray(int rootID, int selectID, bool isSpace, int? delAreaID)
        {
            IList<AreaEntity> randomArealst = new List<AreaEntity>(AreaBLL.GetAreaArray());
            IList<AreaEntity> orderArealst = new List<AreaEntity>();

            AddTreeNode(rootID, rootID, randomArealst, selectID, isSpace, delAreaID, null, ref orderArealst);

            return orderArealst.ToArray();
        }

        public static AreaEntity[] GetAreaStyleArray(int rootID, int selectID, bool isSpace, int? delAreaID, int? level)
        {
            IList<AreaEntity> randomArealst = new List<AreaEntity>(AreaBLL.GetAreaArray());
            IList<AreaEntity> orderArealst = new List<AreaEntity>();

            AddTreeNode(rootID, rootID, randomArealst, selectID, isSpace, delAreaID, level, ref orderArealst);

            return orderArealst.ToArray();
        }

        public static void AddTreeNode(int rootID, int parentID, IList<AreaEntity> randomArealst, int selectID, bool isSpace, int? delAreaID, int? level, ref IList<AreaEntity> orderArealst)
        {
            //过滤ParentID,得到当前的所有子节点
            IList<AreaEntity> areaArray = (from area in randomArealst where area.ParentID == parentID select area).ToArray();

            foreach (AreaEntity area in areaArray)
            {
                if (area.AreaID != delAreaID)
                {
                    if (level != null && area.Level > level) { return; }
                    if (parentID == rootID)
                    {
                        if (area.AreaID == selectID) { area.IsSelect = true; } else { area.IsSelect = false; }
                        //添加根节点
                        orderArealst.Add(area);

                        AddTreeNode(rootID, area.AreaID, randomArealst, selectID, isSpace, delAreaID, level, ref orderArealst);         //再次递归   
                    }
                    else
                    {
                        if (area.AreaID == selectID) { area.IsSelect = true; } else { area.IsSelect = false; }
                        //添加当前节点的子节点
                        if (isSpace) { area.Name = GeLevelEmpty(area.Level) + area.Name; }
                        orderArealst.Add(area);

                        AddTreeNode(rootID, area.AreaID, randomArealst, selectID, isSpace, delAreaID, level, ref orderArealst);          //再次递归   
                    }
                }
            }
        }

        public static string GeLevelEmpty(int level)
        {
            string empty = string.Empty;
            if (level > 0)//判断节点深度 大于1就是表示它是子节点
            {
                for (int i = 0; i < level; i++)
                {
                    empty += "　";//注意这里是个全角空格
                }
                //empty = empty + "|-- ";
            }
            return empty;
        }

        public static AreaEntity GetArea(int areaID)
        {
            return AreaDAL.GetArea(areaID);
        }

        public static string GetAreaName(int areaID)
        {
            return AreaDAL.GetAreaName(areaID);
        }

        public static int InsertArea(AreaEntity area)
        {
            return AreaDAL.InsertArea(area);
        }

        public static int UpdateArea(AreaEntity area)
        {
            return AreaDAL.UpdateArea(area);
        }

        public static int DeleteArea(int areaID)
        {
            return AreaDAL.DeleteArea(areaID);
        }

        public static int SetAreaMoveUp(int areaID)
        {
            return AreaDAL.SetAreaMoveUp(areaID);
        }

        public static int SetAreaMoveDown(int areaID)
        {
            return AreaDAL.SetAreaMoveDown(areaID);
        }
    }
}
